<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: UpRank.php
 * Date: 2017/1/2
 * Time: 23:13
 */
namespace app\api\model\dao;

use think\Model;
use think\Db;

class ConsumerRank extends Model
{
    protected $table = 'consumer_rank';

    protected $pk = 'id';

    /**
     * 增加消费
     *
     */
    public function plusConsumer($userid, $date, $money)
    {
        $sql = sprintf("insert into %s(userid, consumer_value, date, create_time) values(%d,%d,%d,%d) on duplicate key update consumer_value=consumer_value+%d", $this->table, $userid, $money, $date, time(), $money);
        return Db::table($this->table)->query($sql);
    }

    /**
     * 获取人气排行
     *
     */
    public function getConsumerRankByDay($date = 0, $offset = 0, $limit = 10)
    {
        if ($date) {
            $arrCond = [
                'date' => ['=', $date]
            ];
            return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order('consumer_value desc')->select();
        } else {
            return Db::table($this->table)->limit($offset, $limit)->order('consumer_value desc')->select();
        }
    }

    /**
     * 获取按周人气排行
     *
     */
    public function getConsumerRankByWeek($offset = 0, $limit = 10)
    {
        $time = time();
        $N = date('N', $time);
        $monday = date('Ymd', ($time - ($N - 1) * 24 * 3600));
        $date = [];
        for ($i = 0; $i < 7; $i++) {
            $date[] = date('Ymd', (strtotime($monday) + $i * 24 * 3600));
        }

        $sql = sprintf('select userid,sum(consumer_value) as consumer_value from %s where date in (%s) group by userid order by consumer_value desc limit %d,%d', $this->table, implode(',', $date), $offset, $limit);

        return Db::table($this->table)->query($sql);

    }

    /**
     * 获取按月人气排行
     *
     */
    public function getConsumerRankByMonth($offset = 0, $limit = 10)
    {
        $time = time();
        $N = date('j', $time);
        $firstday = date('Ym01', $time);
        $endday = date('Ym01', (strtotime($firstday) + 31 * 24 * 3600));
        $date = [];
        for ($i = 0; $i < 32; $i++) {
            $day = date('Ymd', (strtotime($firstday) + $i * 24 * 3600));
            if ($day < $endday) {
                $date[] = $day;
            }
        }

        $sql = sprintf('select userid,sum(consumer_value) as consumer_value from %s where date in (%s) group by userid order by consumer_value desc limit %d,%d', $this->table, implode(',', $date), $offset, $limit);

        return Db::table($this->table)->query($sql);

    }

    /**
     * 获取按月人气排行
     *
     */
    public function getConsumerRankTotal($offset = 0, $limit = 10)
    {

        $sql = sprintf('select userid,sum(consumer_value) as consumer_value from %s group by userid order by consumer_value desc limit %d,%d', $this->table, $offset, $limit);

        return Db::table($this->table)->query($sql);

    }

    public function consumerRankPage()
    {
        return $this->paginate(10);
    }

}
